package com.example.easybuy.service;

import com.example.easybuy.dao.PhoneDao;
import com.example.easybuy.pojo.Phone;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class PhoneServiceImpl implements PhoneService{
    @Autowired
    private PhoneDao phoneDao;

    private Logger logger=Logger.getLogger(getClass());

    /*
   添加手机订单
    */
    @Override
    public String addPhone(Phone phone) {
        logger.debug("PhoneServiceImpl-addPhone...start...phone:"+phone);
        //查询手机号余额
        Phone phoneMoney =phoneDao.getPhone(phone.getPhoneNum());
        //充值金额
        float recharge=phone.getSurplus();

        int phoneRow=0;
        float money=0;
        if (phoneMoney==null){
            //手机号余额
            money=0;
        }else {
            //手机号余额
            money=phoneMoney.getSurplus();
        }
        //当前余额=余额+充值金额
        float price=phone.getSurplus()+money;
        //插入 当前余额
        phone.setSurplus(price);
        //创建时间
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime=df.format(new Date());
        phone.setCreatetime(nowTime);
        phone.setRecharge(recharge);
        phoneRow=phoneDao.addPhone(phone);

        String success=null;
        if (phoneRow>0){
            success="添加成功";
        }
        logger.debug("PhoneServiceImpl-addPhone-success:"+success);
        return success;
    }

    /*
    查询手机订单
     */
//    @Override
//    public float getPhone(String phoneNum) {
//        logger.debug("PhoneServiceImpl-getPhone...start...phoneNum:"+phoneNum);
//        Phone phone=phoneDao.getPhone(phoneNum);
//        return phone;
//    }

}
